package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.*;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

//import Listeners.SubmitPaperListener;
import Model.Conference;
import Model.Paper;
import Model.User;

/**
 * A JPanel that allows the user to submit a new paper
 * by filling in the paper title, uploading a manuscript
 * File, and then clicking the submit paper button.
 * 
 * @author Paul Schultz (ps87@uw.edu)
 * @author Nathan Judkins (nate001@.uw.edu)
 * @version 12/01/2012
 */
@SuppressWarnings("serial")
public class SubmitPaperPanel extends JPanel
{
  private final ConferenceWizardFrame my_frame;
  private final User my_current_author;
  private final Conference my_conference;
  private final JLabel WELCOME_MESSAGE;
  private final Font DEFAULT_FONT = new Font("Plain", Font.PLAIN, 16);
  private JLabel my_upload_status;
  private JButton my_submit;
  private JTextField my_title;
  private Paper my_paper;
  private File my_manuscript;
  
  /**
   * Constructs a new Panel that displays input
   * fields and buttons for a User to input
   * data and create a new Paper for a conference.
   * 
   * @param the_user The Author of the new paper.
   * @param the_conference The Conference the paper will be added to.
   * @param the_frame The master frame for the application.
   */
  public SubmitPaperPanel(final User the_user, final Conference the_conference
      , final ConferenceWizardFrame the_frame)
  {    
    super();
    my_current_author = the_user;
    my_conference = the_conference;
    my_frame = the_frame;
    
    WELCOME_MESSAGE = new JLabel("Paper Submission Tool");
    my_upload_status = new JLabel("No file found.  Please choose a manuscript to upload.");
    
    
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(800, 600));
    setBackground(Color.WHITE);
      
    WELCOME_MESSAGE.setForeground(Color.BLUE);
    WELCOME_MESSAGE.setFont(new Font("Plain", Font.PLAIN, 38));
    
    setUpNorthPanel();
    setUpCenterPanel();
    setUpSouthPanel();
  }
  
  /**
   * Constructs a new Panel that displays input
   * fields and buttons for a User to edit the
   * data for an existing paper and then submit changes
   * to the paper.
   * 
   * @param the_user The Author of the paper being edited.
   * @param the_conference The Conference the paper will be added to.
   * @param the_frame The master frame for the application.
   * @param the_paper The Paper object being edited.
   */
  public SubmitPaperPanel(final User the_user, final Conference the_conference
      , final ConferenceWizardFrame the_frame, final Paper the_paper)
  {    
    super();
    my_current_author = the_user;
    my_conference = the_conference;
    my_frame = the_frame;
    my_paper = the_paper;
    
    WELCOME_MESSAGE = new JLabel("Edit Paper Tool");
    String title = "No paper uploaded";
    if (the_paper.getManuscript() != null)
    {
      title = the_paper.getManuscript().toString();
    }
    my_upload_status = new JLabel(title);
    
    
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(800, 600));
    setBackground(Color.WHITE);
      
    WELCOME_MESSAGE.setForeground(Color.BLUE);
    WELCOME_MESSAGE.setFont(new Font("Plain", Font.PLAIN, 38));
    
    setUpNorthPanel();
    setUpCenterPanelEdit();
    setUpSouthPanelEdit();
  }
  
  /**
   * Sets up the Northern area of this panel.
   */
  public void setUpNorthPanel()
  {
    JPanel north_panel = new JPanel();
    north_panel.setLayout(new BoxLayout(north_panel, BoxLayout.Y_AXIS));
    north_panel.setBackground(Color.WHITE);
    north_panel.add(WELCOME_MESSAGE);
    add(north_panel, BorderLayout.NORTH);
  }
  
  /**
   * Sets up the center of this panel to show
   * queues of submitting a new paper.
   */
  public void setUpCenterPanel()
  {
    JPanel center_panel = new JPanel();
    center_panel.setLayout(new BoxLayout(center_panel, BoxLayout.Y_AXIS));
    center_panel.setBackground(Color.WHITE);
    
    JPanel title = new JPanel();
    title.setMaximumSize(new Dimension(600, 50));
    JPanel author = new JPanel();
    author.setMaximumSize(new Dimension(600, 50));
    
    JLabel authInfo = new JLabel("Author: " +  my_current_author.realName());
    authInfo.setFont(DEFAULT_FONT);
    
    JLabel tLabel = new JLabel("Paper Title:");
    tLabel.setFont(DEFAULT_FONT);
    JPanel tPanel = new JPanel();
    tPanel.add(tLabel);
    
    my_title = new JTextField("Insert Title Here", 30);
    my_title.setFont(new Font("Plain", Font.PLAIN, 16));
    
    my_upload_status.setForeground(Color.RED);
    my_upload_status.setFont(DEFAULT_FONT);
    
    JLabel file = new JLabel("Manuscript File: ");
    file.setFont(DEFAULT_FONT);
  
    JPanel manuscript = new JPanel();
    manuscript.setMaximumSize(new Dimension(600,50));
    
    manuscript.add(file);
    manuscript.add(my_upload_status);    

    author.add(authInfo);
    title.add(tPanel);
    title.add(my_title);
    
    center_panel.add(Box.createVerticalStrut(150));
    center_panel.add(author);
    center_panel.add(title);
    center_panel.add(manuscript);
    add(center_panel, BorderLayout.CENTER);
    
  }
  
  /**
   * Sets up the south of this panel to show
   * queues of submitting a new paper.
   */
  public void setUpSouthPanel()
  {
    JPanel south_panel = new JPanel();
    south_panel.setBackground(Color.WHITE);
    south_panel.add(Box.createRigidArea(new Dimension(0, 100)));
    
    JButton upload = new JButton("Upload Manuscript");
    upload.addActionListener(new UploadPaperListener()); //set the listener for upload
    
    my_submit = new JButton("Submit Paper");
    my_submit.addActionListener(new SubmitPaperListener());
    my_submit.setEnabled(false); //Set to false until a paper is uploaded
    
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new CancelSubmitListener());
    
    south_panel.add(cancel);
    south_panel.add(upload);
    south_panel.add(my_submit);
    
    add(south_panel, BorderLayout.SOUTH);
  }
  
  /**
   * Sets up the center of this panel to show
   * queues of editing a paper.
   */
  public void setUpCenterPanelEdit()
  {
    JPanel center_panel = new JPanel();
    center_panel.setLayout(new BoxLayout(center_panel, BoxLayout.Y_AXIS));
    center_panel.setAlignmentY(LEFT_ALIGNMENT);
    center_panel.setBackground(Color.WHITE);
    
    JPanel title = new JPanel();
    title.setMaximumSize(new Dimension(600, 50));
    JPanel author = new JPanel();
    author.setAlignmentY(LEFT_ALIGNMENT);
    author.setMaximumSize(new Dimension(600, 50));
    
    JLabel authInfo = new JLabel("Author: " +  my_current_author.realName());
    authInfo.setFont(DEFAULT_FONT);
    
    JLabel tLabel = new JLabel("Paper Title:");
    tLabel.setFont(DEFAULT_FONT);
    JPanel tPanel = new JPanel();
    tPanel.setAlignmentY(LEFT_ALIGNMENT);
    tPanel.add(tLabel);
    
    my_title = new JTextField(my_paper.getTitle(), 30);
    my_title.setFont(new Font("Plain", Font.PLAIN, 16));
    
    my_upload_status.setForeground(new Color(0, 153, 51));
    my_upload_status.setFont(DEFAULT_FONT);
    
    JLabel file = new JLabel("Manuscript File: ");
    file.setFont(DEFAULT_FONT);
    
    JPanel manuscript = new JPanel();
    manuscript.setMaximumSize(new Dimension(600,50));
    
    manuscript.add(file);
    manuscript.add(my_upload_status);    
  
    author.add(authInfo);
    title.add(tPanel);
    title.add(my_title);
    
    center_panel.add(Box.createVerticalStrut(150));
    center_panel.add(author);
    center_panel.add(title);
    center_panel.add(manuscript);
    add(center_panel, BorderLayout.CENTER);
    
  }
  
  /**
   * Sets up the south area of this panel to show
   * queues of editing a paper.
   */
  public void setUpSouthPanelEdit()
  {
    JPanel south_panel = new JPanel();
    south_panel.setBackground(Color.WHITE);
    south_panel.add(Box.createRigidArea(new Dimension(0, 100)));
    
    JButton upload = new JButton("Upload Manuscript");
    upload.addActionListener(new EditUploadListener()); //set the listener for upload
    
    my_submit = new JButton("Submit Edition");
    my_submit.addActionListener(new SubmitEditionListener());
    
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new CancelSubmitListener());
    
    south_panel.add(cancel);
    south_panel.add(upload);
    south_panel.add(my_submit);
    
    add(south_panel, BorderLayout.SOUTH);
  }
  
  /**
   * Listener used for the upload button for
   * submitting a new paper to a conference.
   * 
   * @author Nathan Judkins (nate001@.uw.edu)
   * @version 11/24/2012
   */
  public class UploadPaperListener extends AbstractAction
  {
    /**
     * Populates a JFileChooser so the user can
     * upload a manuscript File.
     */
    public void actionPerformed(ActionEvent arg0)
    {
      JFileChooser paper = new JFileChooser();

      int returnVal = paper.showOpenDialog(null);

      if (returnVal == JFileChooser.APPROVE_OPTION && canSubmit())
      {

        my_paper = new Paper(my_title.getText(), my_current_author, 
            paper.getSelectedFile());
        
        my_upload_status.setText(paper.getSelectedFile().toString());
        my_upload_status.setForeground(new Color(0, 153, 51));
        my_upload_status.repaint();
        
        my_submit.setEnabled(true);
      }
      else if (returnVal == JFileChooser.CANCEL_OPTION)
      {
        // Do Nothing & JFilechooser closes
      }
      else
        JOptionPane.showMessageDialog(null, "You cannot submit anymore Papers");
    }
    
    /**
     * A helper method intended to make the check first if he's is part
     * of this conference and then if he/she is below the 4 paper limit
     * per conference.
     * 
     * @author Nathan Judkins (nate001@.uw.edu)
     * @version 11/24/2012
     * @return Whether or not conference can accept this paper
     */
    private boolean canSubmit()
    {
      boolean canSubmit = true;
      
      if (!my_conference.containsUser(my_current_author))
      {
        my_conference.addUser(my_current_author);
      }
      
      //A check to make sure they haven't approached the limit of Papers.
      List<Paper> authorPapers = my_conference.getSubmittedPapers();
      if (authorPapers.size() > 4)
        canSubmit = false;
     
      return canSubmit;
    } 
  }
  
  /**
   * Listener used for the Upload button when editing a paper.
   * 
   * @author Paul Schultz (ps87@uw.edu)
   * @version 12/01/2012
   */
  public class EditUploadListener extends AbstractAction
  {
    /**
     * Populates a JFilechooser so that the user
     * can upload a manuscript File.
     */
    public void actionPerformed(ActionEvent arg0)
    {
      JFileChooser paper = new JFileChooser();

      int returnVal = paper.showOpenDialog(null);

      if (returnVal == JFileChooser.APPROVE_OPTION)
      {
        my_manuscript = paper.getSelectedFile();
        
        my_upload_status.setText(paper.getSelectedFile().toString());
        my_upload_status.setForeground(new Color(0, 153, 51));
        my_upload_status.repaint();
      }
      else if (returnVal == JFileChooser.CANCEL_OPTION)
      {
        // Do Nothing & JFilechooser closes
      }
      else
        JOptionPane.showMessageDialog(null, "You cannot submit anymore Papers");
    }
  }
  
  /**
   * Listener used for Submit (Paper) button.
   * 
   * @author Paul Schultz (ps87@uw.edu)
   * @version 11/24/2012
   */
  public class SubmitPaperListener extends AbstractAction
  {
    /**
     * Adds the paper to the conference and then returns GUI
     * to the Author Tool Panel.
     */
    public void actionPerformed(ActionEvent arg0)
    {
      my_conference.addPaper(my_paper);
      my_frame.setCurrentPanel(new AuthorToolPanel(my_conference, my_current_author, my_frame));
    }
  }
  
  /**
   * Listener used for Submit Edition button.
   * 
   * @author Paul Schultz (ps87@uw.edu)
   * @version 11/24/2012
   */
  public class SubmitEditionListener extends AbstractAction
  {
    /**
     * Adds the paper to the conference and then returns GUI
     * to the Author Tool Panel.
     */
    public void actionPerformed(ActionEvent arg0)
    {
      my_paper.editManuscript(my_manuscript);
      my_paper.editTitle(my_title.getText());
      
      my_frame.setCurrentPanel(new AuthorToolPanel(my_conference, my_current_author, my_frame));
    }
  }
  
  /**
   * Listener used for the Cancel button when
   * submitting/editing a paper.
   * 
   * @author Paul Schultz (ps87@uw.edu)
   * @version 11/30/2012
   */
  public class CancelSubmitListener extends AbstractAction
  {
    /**
     * Returns the User to the AuthorTool and discards
     * anything stored in fields or file's chosen for upload.
     */
    public void actionPerformed(ActionEvent e)
    {
      my_frame.setCurrentPanel(new AuthorToolPanel(my_conference, my_current_author, my_frame));
    }
  }
}
